/**
 * 
 */
package com.mrroman.linksender;

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.Authenticator;
import java.util.ResourceBundle;

import javax.swing.UIManager;

import com.mrroman.linksender.filesender.ServerManager;
import com.mrroman.linksender.gui.CommonPopupMenu;
import com.mrroman.linksender.gui.MainUI;
import com.mrroman.linksender.gui.PingListener;
import com.mrroman.linksender.gui.PopupPanel;
import com.mrroman.linksender.history.HistoryKeeper;
import com.mrroman.linksender.ioc.ObjectStore;
import com.mrroman.linksender.passdb.PasswordKeeperAuthenticator;
import com.mrroman.linksender.sender.MessageListener;
import com.mrroman.linksender.sender.MessagePoint;
import com.mrroman.linksender.sender.Pinger;
import com.mrroman.linksender.sender.PrivateMessagePoint;
import com.mrroman.linksender.twitter.TwitterMessageListener;

/**
 * @author Konrad
 * 
 */
public class Main {

    public static ObjectStore objStore;

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.getDefaults().put("ToolTipManager.enableToolTipMode","");
            Toolkit.getDefaultToolkit().addAWTEventListener(new CommonPopupMenu(), AWTEvent.MOUSE_EVENT_MASK);
        } catch (Exception e) {
        }

        ObjectStore.getInstance().setMessageBundle(ResourceBundle.getBundle("com.mrroman.linksender.resources.locale"));

        Configuration config = ObjectStore.getObject(Configuration.class);
        
        Authenticator.setDefault(ObjectStore.getObject(PasswordKeeperAuthenticator.class));

        MessagePoint msgPoint = ObjectStore.getObject(MessagePoint.class);
        PrivateMessagePoint privPoint = ObjectStore.getObject(PrivateMessagePoint.class);
        ServerManager shareSever = ObjectStore.getObject(ServerManager.class);
        PopupPanel popupPanel = ObjectStore.getObject(PopupPanel.class);
//        try {
//            config.setPort(8078);
//        } catch (PropertyVetoException ex) {
//        }
        
        msgPoint.setServer(
                config.getAddress(),
                config.getPort());
        privPoint.setServer(config.getPort()+1);
        msgPoint.setLoopbackMode(config.isLoopbackMode());

        shareSever.setParameters(config.getPort()+2, 25, config.homepage(), config.applicationName());
        
        msgPoint.addMessageListener((MessageListener) ObjectStore.getObject(HistoryKeeper.class));
        msgPoint.addMessageListener(popupPanel);
        msgPoint.addMessageListener(ObjectStore.getObject(TwitterMessageListener.class));
        privPoint.addMessageListener(popupPanel);
        privPoint.addMessageListener((MessageListener) ObjectStore.getObject(HistoryKeeper.class));

        Thread thread = new Thread(msgPoint);
        thread.start();
        
        thread = new Thread(privPoint);
        thread.start();

        thread = new Thread(shareSever);
        thread.start();

        PingListener pingListener = ObjectStore.getObject(PingListener.class);
        msgPoint.addMessageListener(pingListener);

        Pinger pinger = ObjectStore.getObject(Pinger.class);
        thread = new Thread(pinger);
        thread.start();

        do {
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                System.exit(-1);
            }
        } while (!msgPoint.isStarted());
   
        ObjectStore.getObject(MainUI.class);
    }
}
